package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.MatRepairDeteMapper;
import com.indusfo.spc.pojo.MatDete;
import com.indusfo.spc.pojo.MatRepairDete;
import com.indusfo.spc.service.MatRepairDeteService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 熊冰 物料维修流程采集项目
 * @Date: 2019/11/25 9:59
 */
@Service
public class MatRepairDeteServiceImpl implements MatRepairDeteService {
	// 声明日志的静态变量
	private static final Logger logger = LoggerFactory.getLogger(MatRepairDeteServiceImpl.class);

	@Resource
	private MatRepairDeteMapper matRepairDeteMapper;
	//查询
	@Override
	public JSONObject queryMatRepairDete(MatRepairDete matRepairDete) {
		try {
			//获取页面的数据个数
			Integer pagesize = matRepairDete.getPagesize();
			//获取查询的页数
			Integer pageindex = matRepairDete.getPageindex();
			if(pagesize!=null && pageindex !=null){
				matRepairDete.setIncept(pagesize*(pageindex-1));
			}
			//执行查询操作
			List<MatRepairDete> matRepairDeteList =new ArrayList<>();
			if(matRepairDete.getMatRepairId()==null){
				return JSONObject.oK(HintMessage.QUERY_NULL, matRepairDeteList, 0);
			}
			 matRepairDeteList=matRepairDeteMapper.queryMatRepairDete(matRepairDete);

			//如果页数和页面数量都不为空的情况下进行计数
			if(matRepairDeteList.isEmpty()){
				return  JSONObject.oK(HintMessage.QUERY_NULL,matRepairDeteList,0);
			}else{
				Integer count= matRepairDeteMapper.countMatRepairDete(matRepairDete);
				return JSONObject.oK(HintMessage.QUERY_SUCCESS,matRepairDeteList,count);
			}

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}
	//新增
	@Override
	public JSONObject insertMatRepairDete(MatRepairDete matRepairDete) {
		try{
			if(matRepairDete.getMatRepairDeteId()!=null){
				throw new ModifyFailedException(HintMessage.INSERT_ID);
			}
			//初始化基础数据
			if(matRepairDete.getDataState()==null){
				matRepairDete.setDataState(1);
			}
			//判重
			checkParam(matRepairDete);
			//执行新增操作
			Integer row=matRepairDeteMapper.insertMatRepairDete(matRepairDete);
			if(row==0){
				throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
			}
			return JSONObject.oK(HintMessage.INSERT_SUCCESS);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//修改
	@Override
	public JSONObject updateMatRepairDete(MatRepairDete matRepairDete) {
		try {
			if(matRepairDete.getMatRepairDeteId()==null) {
				throw new ParamsErrorException(HintMessage.UPDATE_ID);
			}
			//初始化基础数据
			if(matRepairDete.getDataState()==null){
				matRepairDete.setDataState(1);
			}
			//判重
			checkParam(matRepairDete);
			//执行修改操作
			int row = matRepairDeteMapper.updateMatRepairDete(matRepairDete);
			if(row == 0) {
				throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
			}
			return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
		}catch (GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//删除
	@Override
	public JSONObject deleteMatRepairDete(List<Integer> matRepairDeteIds, Integer dataState) {
		try{
			//空值判断
			if(dataState == null) {
				throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
			}
			if(matRepairDeteIds == null) {
				throw new ParamsErrorException(HintMessage.LIST_NULL);
			}
			//记录异常信息
			String msg = "";
			switch (dataState) {
				case 1:
					msg = HintMessage.START_VALUE;break;
				case 2:
					msg = HintMessage.DELETE_VALUE;break;
				case 3:
					msg = HintMessage.STOP_VALUE;break;
				default:
					throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
			}
			//执行删除操作
			int row = matRepairDeteMapper.deleteMatRepairDete(matRepairDeteIds,dataState);
			//声明msg字符串，用来存抛出的异常
			if (row == 0) {
				msg=msg+ HintMessage.DEFEATED_VALUE;
			} else {
				msg=msg+ HintMessage.SUCCESS_VALUE;
			}
			return JSONObject.oK(msg);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	//判重
	public void checkParam(MatRepairDete matRepairDete){

		//上限
		String upperLimit = matRepairDete.getUpperLimit();
		//下限
		String lowerLimit = matRepairDete.getLowerLimit();
		//中心值
		String middleLimit = matRepairDete.getMiddleLimit();
		//维修流程ID
		Integer matRepairId = matRepairDete.getMatRepairId();
		//采集参数ID
		Integer deteId = matRepairDete.getDeteId();
		//维修流程明细ID
		Integer repairDetailId = matRepairDete.getRepairDetailId();
		//判空
		//产品流程ID
		if (matRepairId == null) {
			throw new ParamsErrorException(HintMessage.MATREPAIRID_NULL);
		}
		//采集参数ID
		if (deteId == null) {
			throw new ParamsErrorException(HintMessage.DETEID_NULL);
		}

		if (repairDetailId == null) {
			throw new ParamsErrorException(HintMessage.REPAIRDETAILID_NULL);
		}
		//上下限数值判断
		if (StringUtils.isEmpty(upperLimit)) {
			throw new ParamsErrorException(HintMessage.UPPERLIMIT_NULL);
		}
		if (StringUtils.isEmpty(lowerLimit)) {
			throw new ParamsErrorException(HintMessage.LOWERLIMIT_NULL);
		}
		if (StringUtils.isEmpty(middleLimit)) {
			throw new ParamsErrorException(HintMessage.MIDDLELIMIT_NULL);
		}
		if (upperLimit.getBytes().length > 50) {
			throw new ParamsErrorException(HintMessage.UPPERLIMIT_SIZE);
		}
		if (lowerLimit.getBytes().length > 50) {
			throw new ParamsErrorException(HintMessage.LOWERLIMIT_SIZE);
		}
		if (middleLimit.getBytes().length > 50) {
			throw new ParamsErrorException(HintMessage.MIDDLELIMIT_SIZE);
		}
		if (Double.parseDouble(upperLimit) <= Double.parseDouble(lowerLimit)) {
			throw new ParamsErrorException(HintMessage.UPPERLIMIT_LOWERLIMIT);
		}
		if (Double.parseDouble(middleLimit) < Double.parseDouble(lowerLimit)) {
			throw new ParamsErrorException(HintMessage.MIDDLELIMIT_LOWERLIMIT);
		}
		if (Double.parseDouble(middleLimit) > Double.parseDouble(upperLimit)) {
			throw new ParamsErrorException(HintMessage.MIDDLELIMIT_UPPERLIMIT);
		}
		//判断字符长度


		// 执行判重操作
		Integer Counts = matRepairDeteMapper.checkMatRepairDete(matRepairDete);
		if (Counts !=0) {
			throw new ParamsErrorException(HintMessage.REPEATING_DATA);
		}

	}
}
